package com.wondertek.poms.core.filter.impl;

import com.wondertek.poms.core.filter.BaseRuleFilter;
import com.wondertek.poms.core.filter.RuleFilterChain;
import org.apache.commons.lang3.StringUtils;

import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.IRulePo;
import com.wondertek.poms.dao.po.PcProductRule;
import com.wondertek.poms.dao.po.PrdInfoRule;


/**
 * <p><b>CopyRightTerminalFilter Description:</b> 版权终端过滤器 </p>
 * @author douzi
 * <b>DATE</b> 2019年9月27日 下午4:06:34
 */
public class CopyRightTerminalFilter extends BaseRuleFilter {

	@Override
	public void doFilter(IRulePo rule, Content c, RuleFilterChain chain) {
		if (rule instanceof PrdInfoRule) {
			PrdInfoRule r = (PrdInfoRule) rule;
			if (r.getIsprdSel() == 2) {
				this.specialFilter(
						r.getPrdRuleId(), 
						r.getCopytypeTal(), 
						c.getTerminal(), 
						chain, 
						"版权终端-Terminal");
			} else if (r.getIsprdSel() == 4) {
				this.terminalLimitFilter(
						r.getPrdRuleId(),
						r.getCopytypeTal(),
						c.getTerminal(),
						chain,
						"版权终端-Terminal");
			}
		} else if (rule instanceof PcProductRule) {
            PcProductRule r= (PcProductRule) rule;
            if ("1".equals(r.getRuleType())) {
				this.specialFilter(
						r.getId(),
						r.getTerminal(),
						c.getTerminal(),
						chain,
						"版权终端-Terminal");
			} else if ("2".equals(r.getRuleType())) {
				this.terminalLimitFilter(
						r.getId(),
						r.getTerminal(),
						c.getTerminal(),
						chain,
						"版权终端-Terminal");
			}
        }
		chain.doFilter(rule, c, chain);
	}

	/**
	 * <p><b>Title:</b> terminalLimitFilter</p>
	 * <p><b>Description:</b> 限制规则 （规则完全包含内容则匹配（于POMS正好相反），
	 * 				如规则是1、2，内容是2、3，则内容不匹配这条规则；如规则是1、2、3，内容是2、3，则内容匹配这条限制规则，不允许进包。）</p>
	 * @author douzi
	 * @param prdRuleId
	 * @param copytypeTal
	 * @param terminal
	 * @param chain
	 * @param string
	 */
	private void terminalLimitFilter(Long id, String ruleStr, String voStr, RuleFilterChain chain, String comments) {
		if (chain.isFlag() && StringUtils.isNotBlank(ruleStr)) {
			if (StringUtils.isNotBlank(voStr)) {
				String ruleTemp = ruleStr.trim().replaceAll("，", ",");
				String voTemp = voStr.trim().replaceAll("，", ",");
				ruleTemp = "," + ruleTemp + ",";
				String[] voTemps = voTemp.split("\\|");
				for (String vo : voTemps) {
					vo = "," + vo + ",";
					if (!ruleTemp.contains(vo)) {
						chain.setFlag(Boolean.FALSE);
						break;
					}
				}
			}

			log(id, chain, comments, ruleStr, voStr);
		}
	}
}
